﻿using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class NewRenController : BaseController
    {
        // GET: NewRen
        public ActionResult Index(string City, string District, string Town, string Job, int p = 0)
        {
            try
            {
                IEnumerable<User> query = db.Users.AsEnumerable();
                if (!string.IsNullOrEmpty(City))
                {
                    query = query.Where(c => c.City == City);
                }
                if (!string.IsNullOrEmpty(District))
                {
                    query = query.Where(c => c.District == District);
                }
                if (!string.IsNullOrEmpty(Town))
                {
                    query = query.Where(c => c.Town == Town);
                }
                if (!string.IsNullOrEmpty(Job))
                {
                    query = query.Where(c => c.Job == Job);
                }
                ViewBag.City = (City == null ? "" : City);
                ViewBag.District = (District == null ? "" : District);
                ViewBag.Town = (Town == null ? "" : Town);
                ViewBag.Job = (Job == null ? "" : Job);
                query = query.OrderByDescending(x => x.Priority);
                ViewBag.PageInfo = PagerHelper.Do(ref query, 50, p);

                List<City> Cities = new List<City>();
                List<Job> Jobs = new List<Job>();
                List<District> Districts = new List<District>();
                List<Town> Towns = new List<Models.Town>();
                List<City> CitiesMore = new List<City>();
                List<Job> JobsMore = new List<Job>();
                List<District> DistrictsMore = new List<District>();
                List<Town> TownsMore = new List<Models.Town>();

                Cities = db.Cities.DistinctBy(c => c.CityName).Take(20).ToList();
                CitiesMore = db.Cities.DistinctBy(c => c.CityName).Skip(20).ToList();
                Jobs = db.Jobs.DistinctBy(j => j.JobName).Take(10).ToList();
                JobsMore = db.Jobs.DistinctBy(j => j.JobName).Skip(10).ToList();
                Districts = db.Districts.DistinctBy(d => d.DistrictName).Take(20).ToList();
                DistrictsMore = db.Districts.DistinctBy(d => d.DistrictName).Skip(20).ToList();
                Towns = db.Towns.DistinctBy(t => t.TownName).Take(20).ToList();
                TownsMore = db.Towns.DistinctBy(t => t.TownName).Skip(20).ToList();

                ViewBag.Cities = Cities;
                ViewBag.Jobs = Jobs;
                ViewBag.Districts = Districts;
                ViewBag.Towns = Towns;
                ViewBag.CitiesMore = CitiesMore;
                ViewBag.JobsMore = JobsMore;
                ViewBag.DistrictsMore = DistrictsMore;
                ViewBag.TownsMore = TownsMore;
                return View(query);
            }
            catch (Exception ex)
            {
                log.Error(new LogContent("首页出错", HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=网络繁忙，请稍后重试");
            }
        }
    }
}